// -----------------------------------------------------------------------
// <copyright file="Default.aspx.cs" company="Imar.Spaanjaars.Com">
//   Copyright 2008 - 2009 - Imar.Spaanjaars.Com. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Web.Security;
using System.Web.UI.WebControls;
using Spaanjaars.ContactManager.Bll;
using Spaanjaars.ContactManager.BusinessEntities;
using Spaanjaars.ContactManager.Web;

public partial class _Default : BasePage
{
	protected void gvContactPersons_RowCommand(object sender, GridViewCommandEventArgs e)
	{
		switch (e.CommandName.ToLower())
		{
			case "addresses":
				gvContactPersons.SelectedIndex = Convert.ToInt32(e.CommandArgument);
				MultiView1.ActiveViewIndex = 0;
				break;
			case "emailaddresses":
				gvContactPersons.SelectedIndex = Convert.ToInt32(e.CommandArgument);
				MultiView1.ActiveViewIndex = 1;
				break;
			case "phonenumbers":
				gvContactPersons.SelectedIndex = Convert.ToInt32(e.CommandArgument);
				MultiView1.ActiveViewIndex = 2;
				break;
			case "edit":
				int contactPersonId = Convert.ToInt32(e.CommandArgument);
				Response.Redirect(string.Format("AddEditContactPerson.aspx?Id={0}", contactPersonId.ToString()));
				break;
		}
	}

	protected void btnNew_Click(object sender, EventArgs e)
	{
		Response.Redirect("AddEditContactPerson.aspx");
	}

	protected void fv_ItemInserting(object sender, FormViewInsertEventArgs e)
	{
		// Assign the selected contact person's ID to the ContactPersonId key in the dictionary
		e.Values["ContactPersonId"] = Convert.ToInt32(gvContactPersons.SelectedDataKey.Value);
	}

	#region Addresses

	protected void btnNewAddress_Click(object sender, EventArgs e)
	{
		ShowAddressInsert();
	}

	private void ShowAddressList()
	{
		gvAddresses.Visible = true;
		fvAddress.Visible = false;
		ErrorList1.Visible = false;
	}

	private void ShowAddressInsert()
	{
		gvAddresses.Visible = false;
		fvAddress.Visible = true;
	}

	protected void fvAddress_ItemCommand(object sender, FormViewCommandEventArgs e)
	{
		switch (e.CommandName.ToLower())
		{
			case "cancel":
				ShowAddressList();
				break;
		}
	}

	protected void fvAddress_ItemInserted(object sender, FormViewInsertedEventArgs e)
	{

	}

	#endregion

	#region E-mail Addresses

	protected void btnNewEmailAddress_Click(object sender, EventArgs e)
	{
		ShowEmailAddressInsert();
	}

	private void ShowEmailAddressList()
	{
		gvEmailAddresses.Visible = true;
		fvEmailAddress.Visible = false;
	}

	private void ShowEmailAddressInsert()
	{
		gvEmailAddresses.Visible = false;
		fvEmailAddress.Visible = true;
	}

	protected void fvEmailAddress_ItemCommand(object sender, FormViewCommandEventArgs e)
	{
		switch (e.CommandName.ToLower())
		{
			case "cancel":
				ShowEmailAddressList();
				break;
		}
	}

	protected void fvEmailAddress_ItemInserted(object sender, FormViewInsertedEventArgs e)
	{
		ShowEmailAddressList();
	}


	#endregion

	#region Phone Numbers


	protected void btnNewPhoneNumber_Click(object sender, EventArgs e)
	{
		ShowPhoneNumberInsert();
	}

	private void ShowPhoneNumberList()
	{
		gvPhoneNumbers.Visible = true;
		fvPhoneNumber.Visible = false;
	}

	private void ShowPhoneNumberInsert()
	{
		gvPhoneNumbers.Visible = false;
		fvPhoneNumber.Visible = true;
	}

	protected void fvPhoneNumber_ItemCommand(object sender, FormViewCommandEventArgs e)
	{
		switch (e.CommandName.ToLower())
		{
			case "cancel":
				ShowPhoneNumberList();
				break;
		}
	}

	protected void fvPhoneNumber_ItemInserted(object sender, FormViewInsertedEventArgs e)
	{
		ShowPhoneNumberList();
	}


	#endregion

	protected void odsAddresses_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
	{
		BusinessBase myBusinessBase = e.InputParameters[0] as BusinessBase;
		if (!myBusinessBase.Validate())
		{
			e.Cancel = true;
			ErrorList1.BrokenRules = myBusinessBase.BrokenRules;
			fvAddress.Visible = true;
		}
	}
	protected void odsAddresses_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
	{
		ShowAddressList();
	}
	protected void fvAddress_ItemUpdating(object sender, FormViewUpdateEventArgs e)
	{

	}
	protected void gvAddresses_RowUpdating(object sender, GridViewUpdateEventArgs e)
	{
		if (ViewState["AddressConcurrenceId"] != null)
		{
			e.NewValues["ConcurrencyId"] = (byte[])ViewState["AddressConcurrenceId"];
			ViewState["AddressConcurrenceId"] = null;
		}
	}

	protected void btnOverwrite_Click(object sender, EventArgs e)
	{
		Address myAddress = AddressManager.GetItem((int)gvAddresses.DataKeys[gvAddresses.EditIndex].Values[0]);
		ViewState["AddressConcurrenceId"] = myAddress.ConcurrencyId;
		gvAddresses.UpdateRow(gvAddresses.EditIndex, true);
		gvAddresses.EditIndex = -1;
	}

	protected void btnReload_Click(object sender, EventArgs e)
	{
		gvAddresses.DataBind();
	}

	protected void gvAddresses_RowUpdated(object sender, GridViewUpdatedEventArgs e)
	{
		if (e.Exception != null && e.Exception.InnerException is DBConcurrencyException)
		{
			e.ExceptionHandled = true;
			e.KeepInEditMode = true;
			plcConcurrency.Visible = true;
		}
		else
		{
			plcConcurrency.Visible = false;
		}
	}

	protected void Page_Load(object sender, EventArgs e)
	{
		btnNew.Visible = Membership.GetUser() != null;
	}

	protected void gvContactPersons_RowCreated(object sender, GridViewRowEventArgs e)
	{
		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			bool userIsAdmin = Roles.IsUserInRole("Administrators");
			bool userIsEditor = Roles.IsUserInRole("Editors");
			LinkButton editButton = e.Row.FindControl("lnkEdit") as LinkButton;
			if (editButton != null)
			{
				editButton.Visible = userIsAdmin || userIsEditor;
			}
			LinkButton deleteButton = e.Row.FindControl("lnkDelete") as LinkButton;
			if (deleteButton != null)
			{
				deleteButton.Visible = userIsAdmin;
			}
		}
	}
}

